package com.car.service;

import com.car.dao.CarDao;
import com.car.dao.UserDao;
import com.car.dao.impl.UserCar;
import com.car.domain.Car;
import com.car.domain.PurchaseRecord;
import com.car.domain.User;
import jdk.internal.jimage.ImageStrings;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

public class UserService {
     private static final UserDao userDao= (UserDao) new UserDaoImpl();

    /**
     * 注册
     */
    public boolean register() throws Exception {
        Scanner scanner = new Scanner(System.in );
        User user=new User();
        System.out.println("用户注册");

        System.out.println("请输入账号用户名");
        String name= scanner.next();
        user.setName(name);

        System.out.println("请输入密码");
        String password= scanner.next();
        user.setName(password);

        System.out.println("请输入手机号: ");
        String phone= scanner.next();
        user.setName(phone);

        System.out.println("请输入Email");
        String email= scanner.next();
        user.setName(email);

        //判断手机号是否已经存在在数据库
        User dbUser = userDao.findByPhone(phone);
        if (dbUser !=null){
            System.out.println("注册失败,手机已经存在");
        }else {
            int i =userDao.add(user);
            if (i != 0){
                System.out.println("注册成功,请登陆.");
            }else {
                System.out.println("注册失败");
            }
        }
        return false;
    }

    public User login(Scanner scanner) {
        System.out.println("用户登陆");
        System.out.println("请输入手机号");
        String phone = scanner.next();
        System.out.println("请输入密码: ");
        String password = scanner.next();
        User user = userDao.login(phone, password);
        return null;
    }
    public void buyCar(User user,Scanner scanner) throws Exception {
        System.out.println("购买汽车");
        //让用户输入汽车的id
        System.out.println("请输入购买汽车的id");
        int carId = scanner.nextInt();
        //判断汽车是否存在并且未销售
        //select*from t_car where status = 0 and id = ?
        CarDao carDao = null;
        Car car =carDao.findByIdAndStatusEq0(carId);
        //购买
        //汽车状态修改  购买记录表新增记录  汽车中间表新增数据
        car.setStatus(0);
        carDao.update(car);
        //购买记录表新增记录
        UserCar userCar= new UserCar();
        userCar.setCarId(carId);
        userCar.setUserId(user.getId());
        ImageStrings userCarDao = null;
        userCarDao.add(userCar.toString());
        //购买记录表新增记录
        PurchaseRecord pr=new PurchaseRecord();
        pr.setCarId(carId);
        pr.setUserId(user.getId());
        pr.setEmployeeId(car.getEmployeeId());
        pr.setPurchaseDatetime(LocalDateTime.now());
    }

    public void updateUserName(User user) {
        System.out.println("修改密码");
        System.out.println("请输入你要修改的密码: ");
        String password = null;
        String username
                = null;
        user.setName(username);
        username.split(String.valueOf(user));
    }

    public void updatePassword(User user, Scanner scanner, String username) {
        System.out.println("修改用户名");
        System.out.println("请输入新的用户名: ");
        String password = scanner.next();
        user.setName(username);
        username.split(String.valueOf(user));
    }

    public List<Car> ownCars(User user) {
        return null;
    }
}
